package com.ynny.chapter05_06.dao;


import com.ynny.chapter05_06.bean.Score;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;

import javax.swing.plaf.PanelUI;
import java.util.List;
@Controller("ScoreMage")
public class ScoreManageImpl implements ScoreManage{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int insertScore(Score score) {
        String sql = "insert into scores (number,Chinese,math,English)value (?,?,?,?)";
        Object []  obj = new Object[]{
                score.getNumber(),
                score.getChinese(),
                score.getMath(),
                score.getEnglish()
        };
        int num = jdbcTemplate.update(sql, obj);
        if (num <= 0){
            System.out.println("插入学生成绩记录失败");
        }
        return  num ;
    }

    @Override
    public int updateScore(String number, String course, Double score) {
        String sql  = "update score set "+ course +"=? where number=?";
        Object [] obj = new Object[]{
                score,number
        };
        int num = jdbcTemplate.update(sql,obj);
        if (num <=0){
            System.out.println("修改学生成绩失败");
        }
        return num;
    }

    @Override
    public Score selectScore(String number) {
        String sql = "select * from scores where number=?";
        RowMapper<Score> rowMapper = new BeanPropertyRowMapper<Score>(Score.class);
        Score score = jdbcTemplate.queryForObject(sql, rowMapper, number);
        return score;
    }

    @Override
    public List<Score> selectAllScore() {
        String sql = "select * from scores";
        RowMapper<Score> rowMapper = new BeanPropertyRowMapper<Score>(Score.class);
        List<Score> scores = jdbcTemplate.query(sql, rowMapper);
        for (Score score :scores){
            System.out.println(score);
        }
        return scores;
    }
}